// ##############################
// ####  note:  bokeweilai event main fun
// ####  create:  TANWENHUI (twh:谭文辉)
// ####  date:   2024/6/18
// ##############################

#include "bkwl_common.h"
#include "bkwl_event.h"
#include "bkwl_network.h"
#include "bkwl_mqtt.h"
#include "bkwl_bt.h"
#include "bkwl_uart.h"
#include "bkwl_doorbell_main.h"
#include "bkwl_camera.h"
#include "bkwl_http.h"
#include "bkwl_str.h"
#include "bkwl_lcd.h"
#include "bkwl_data.h"

bk_err_t bkwl_event_init()
{
	bk_err_t ret = BK_OK;
	tprintf("bkwl_event_init entry\n");
	tprintf("\n\n");
	tprintf("================================================\n");
	tprintf("bkwl_event_init cur version=%s\n", BKWL_IOT_VER_CUR);
	tprintf("================================================\n");
	tprintf("\n\n");
	ret = rtos_create_thread(&bkwl_main->task.event,
							 BEKEN_DEFAULT_WORKER_PRIORITY,
							 "task.event",
							 (beken_thread_function_t)bkwl_event_main_handle,
							 1024 * 2,
							 NULL);

	if (ret != BK_OK)
	{
		tprintf("create media major thread fail\n");
		goto error;
	}
	tprintf("bkwl_event_init exit\n");
error:
	return ret;
}

bk_err_t bkwl_event_send_cmd(uint16_t cmd)
{
	bk_err_t ret = BK_OK;
	bkwl_msg_t msg;
	msg.param = 0;
	msg.event = cmd;
	bkwl_event_send_msg(&msg);
	return ret;
}

bk_err_t bkwl_event_send_msg(bkwl_msg_t *msg)
{
	bk_err_t ret = BK_OK;

	if (bkwl_main->queue.msg)
	{
		ret = rtos_push_to_queue(&bkwl_main->queue.msg, msg, BEKEN_NO_WAIT);

		if (BK_OK != ret)
		{
			tprintf("#### twh bkwl_event_send_msg failed, cmd=%d \n", msg->event);
			return BK_FAIL;
		}

		return ret;
	}

	return ret;
}
bk_err_t bkwl_event_board_init()
{
	bk_err_t ret = BK_OK;
	bkwl_bluetooth_init();
	bkwl_uart_init();

	return ret;
}
// int mqtt_client_example(const char *host_name, const char *username,
// 						const char *password, const char *topic);

// int mqtt_cmd_msg_send(char *topic, char *msg);

bk_err_t bkwl_event_message_handle(void)
{
	bk_err_t ret = BK_OK;
	bkwl_msg_t msg;

	while (1)
	{

		ret = rtos_pop_from_queue(&bkwl_main->queue.msg, &msg, BEKEN_WAIT_FOREVER);

		if (kNoErr == ret)
		{
			tprintf("twh bkwl_event_message_handle get cmd=%d\n", msg.event);
			switch (msg.event)
			{
			case BKWL_WIFI_STATION_CONNECT:
			{
				tprintf("twh main event DBEVT_WIFI_STATION_CONNECT\n");
				// char *ssid = "Bokeweilai";
				// char *key = "BKWL666666";

				// char *ssid = "belon-2.4";
				// char *key = "belon83740372";

				// char *ssid = "BELON";
				// char *key = "belon83740372";

				// char *ssid = "wifi-2.4G";
				// char *key = "12345678";

#if 0
				// char *ssid = "wifi-2.4G";
				// char *key = "12345678";

				char *ssid = "rbq13";
				char *key = "12345678";

				bkwl_network_wifi_sta_connect(ssid, key);
#else
				doorbell_boarding_info_t *doorbell_boarding_info = (doorbell_boarding_info_t *)msg.param;

				strcpy(bkwl_main->bt.ssid, doorbell_boarding_info->boarding_info.ssid_value);
				strcpy(bkwl_main->bt.password, doorbell_boarding_info->boarding_info.password_value);

				bkwl_network_wifi_sta_connect(bkwl_main->bt.ssid,
											  bkwl_main->bt.password);

				// bkwl_network_wifi_sta_connect(doorbell_boarding_info->boarding_info.ssid_value,
				// 							  doorbell_boarding_info->boarding_info.password_value);
#endif
			}
			break;
			case BKWL_WIFI_STATION_BKWL_CONNECT:
			{
				tprintf("twh main event BKWL_WIFI_STATION_BKWL_CONNECT\n");
				doorbell_boarding_info_t *doorbell_boarding_info = (doorbell_boarding_info_t *)msg.param;
				tprintf("twh main get bt pair data:\n%s\n", doorbell_boarding_info->boarding_info.password_value);
				bkwl_get_bt_pair_data(doorbell_boarding_info->boarding_info.password_value);
				if (strlen(bkwl_main->bt.ssid) > 0 && strlen(bkwl_main->bt.password) > 0)
				{
					bkwl_network_wifi_sta_connect(bkwl_main->bt.ssid,
												  bkwl_main->bt.password);
				}

				bkwl_send_bt_pair_ack();
				// bkwl_network_wifi_sta_connect(doorbell_boarding_info->boarding_info.ssid_value,
				//							  doorbell_boarding_info->boarding_info.password_value);
				break;
			}
			case BKWL_WIFI_DATA_CONNECT:
			{
				tprintf("twh main event DBEVT_WIFI_STATION_CONNECT\n");
				// char *ssid = "Bokeweilai";
				// char *key = "BKWL666666";

				// char *ssid = "belon-2.4";
				// char *key = "belon83740372";

				// char *ssid = "BELON";
				// char *key = "belon83740372";

				// char *ssid = "wifi-2.4G";
				// char *key = "12345678";

#if 1
				// char *ssid = "wifi-2.4G";
				// char *key = "12345678";

				// char *ssid = "rbq13";
				// char *key = "12345678";
				char *ssid = "Bokeweilai";
				char *key = "BKWL666666";

				bkwl_network_wifi_sta_connect(ssid, key);
#else
				doorbell_boarding_info_t *doorbell_boarding_info = (doorbell_boarding_info_t *)msg.param;
				bkwl_network_wifi_sta_connect(doorbell_boarding_info->boarding_info.ssid_value,
											  doorbell_boarding_info->boarding_info.password_value);
#endif
			}
			break;
			case BKWL_WIFI_STATION_CONNECTED:
			{
				tprintf("twh main event BKWL_WIFI_STATION_CONNECTED\n");
				netif_ip4_config_t ip4_config;
				extern uint32_t uap_ip_is_start(void);
				os_memset(&ip4_config, 0x0, sizeof(netif_ip4_config_t));
				bk_netif_get_ip4_config(NETIF_IF_AP, &ip4_config);
				if (uap_ip_is_start())
					bk_netif_get_ip4_config(NETIF_IF_AP, &ip4_config);
				else
					bk_netif_get_ip4_config(NETIF_IF_STA, &ip4_config);
				tprintf("\n\n\nip: %s\n", ip4_config.ip);

#if 1
				bkwl_event_send_cmd(BKWL_CMD_INIT_MQTT);
#else
				char *host_name = "120.79.69.20";
				char *username = "tuiyun";
				char *password = "tuiyun123";
				char *topic = "/bkwl/server/data/sn_123";
				bkwl_mqtt_client_init(host_name, username, password, topic);
#endif
			}
			break;
			case BKWL_CMD_MQTT_SEND_DATA:
			{
				// tprintf("twh main event BKWL_CMD_MQTT_SEND_DATA\n");
#if 0
				char *topic_send = "/bkwl/client/data";
				bkwl_mqtt_cmd_msg_send(topic_send, "aaaaaaa");
#else
				bkwl_mqtt_sibo_heartbeat_send();
				// 属性发送
				char *type = "01";
				char *data = "0.95";
				bkwl_mqtt_sibo_property_send(type, data);

				char *user = "01";
				char *utype = "1";
				// 事件上报
				//  {"ver":"1.0.0","act":"0004","type":"01","data":"","user":"01","utype":"1","seq_no":"xxxxxxxxxx"} 
				bkwl_mqtt_sibo_notify_send(type, user, data, utype);

				// {"ver":"1.0.0","act":"0005","sta":"00","seq_no":"xxxxxxxxxx"} 
				// char *name = "sta";
				// char *data1 = "05";
				// bkwl_mqtt_sibo_notify_reply_send(name, data1);

#endif

				break;
			}
			case BKWL_CMD_BOARD_INIT:
			{
				bkwl_event_board_init();
				char *lcd_type_name = "h050iwv";
				media_ppi_t ppi = PPI_800X480;
				bkwl_lcd_init(lcd_type_name, ppi);
				break;
			}
			case BKWL_CMD_CAMERA_INIT:
			{
				tprintf("twh main event BKWL_CMD_CAMERA_INIT\n");
				bkwl_camera_uvc_init();

				break;
			}
			case BKWL_CMD_HTTP_POST_IMAGE:
			{
				tprintf("twh main event BKWL_CMD_HTTP_POST_IMAGE\n");

#if 0
				char *url_upload = "https://m.yo-jia.cn/api/wx/upload/upload.php";
				char *picture_data = "aaaaa";
				int picture_length = 5;
				bkwl_http_upload_picture(url_upload, picture_data, picture_length);

				char *downfile = "https://www.baidu.com/img/flexible/logo/pc/result.png";
				bkwl_http_download_file(downfile);
#endif

#if 0
				// char *url_get_time = "http://m.yo-jia.cn/api/wx/mqtt/getTime.php";
				// bkwl_http_get(url_get_time);
				// char *url_post_login = "http://m.yo-jia.cn/api/wx/applet/phone/login";
				char *url_post_data = "http://m.yo-jia.cn/api/wx/upload/upload.php";
				// char *url_post_data = "http://192.168.1.187:7303/upload/upload.php";
				uint8_t *post_data = (uint8_t *)"aaaaaaa";
				uint32_t data_len = strlen((char *)post_data);
				char *device_name = "D_bFbWajDKqfx4C4am";
				// bkwl_http_post_formdata(url_post_data, post_data, data_len);
				bkwl_http_post_filedata(url_post_data, post_data, data_len, device_name);
				// char *url_post_data = "http://m.yo-jia.cn/api/wx/test/test.php";
				// char *post_data = "{}";
				// uint32_t data_len = strlen(post_data);
				// tprintf("twh main event 1\n");
				// bkwl_http_post(url_post_data, post_data, data_len);
				// char *downfile = "https://www.baidu.com/img/flexible/logo/pc/result.png";
				// bkwl_http_get(downfile);
#endif

				bkwl_mqtt_sibo_upload_image();
				bkwl_doorbell_post_image();
				break;
			}
			case BKWL_CMD_INIT_MQTT:
			{

#if 0
				// bkwl_doorbell_init_mqtt();
#else

				// char *host_name = "120.79.69.20";
				// char *username = "tuiyun";
				// char *password = "tuiyun123";
				// char *topic = "/bkwl/server/data/sn_123";

				strcpy(bkwl_main->mqtt.host, "m.yo-jia.cn");
				tprintf("default host=%s\n", bkwl_main->mqtt.host);
				strcpy(bkwl_main->topic.MQTT_PROPERTY_SERVICE_SET, "/sys/P_1jOMUMWtsEeP8PLP/D_OSdzmGueXwlln1cY/service/property/set");
				tprintf("default topic=%s\n", bkwl_main->topic.MQTT_PROPERTY_SERVICE_SET);
				strcpy(bkwl_main->mqtt.device_name, "D_OSdzmGueXwlln1cY");
				tprintf("default device_name=%s\n", bkwl_main->mqtt.device_name);
				strcpy(bkwl_main->mqtt.product_key, "P_1jOMUMWtsEeP8PLP");
				tprintf("default product_key=%s\n", bkwl_main->mqtt.product_key);
				strcpy(bkwl_main->mqtt.device_secret, "gXd9pmCavN8VP2qZVguPjuQZzaPtD0R0");
				tprintf("default device_secret=%s\n", bkwl_main->mqtt.device_secret);
				tprintf("bkwl_doorbell_init_mqtt product_key=%s\n", bkwl_main->mqtt.product_key);
				bkwl_data_mqtt_init(bkwl_main->mqtt.device_name, bkwl_main->mqtt.product_key);
				bkwl_mqtt_client_init(bkwl_main->mqtt.host, bkwl_main->mqtt.device_name, bkwl_main->mqtt.device_secret, bkwl_main->topic.MQTT_PROPERTY_SERVICE_SET);

#endif
			}
			case BKWL_CMD_LCD_DISPLAY:
			{
				// bkwl_lcd_display_red();
				break;
			}
			}
		}
	}
}

bk_err_t bkwl_event_main_handle(void *para)
{

	bk_err_t ret = BK_OK;
	int32 num = 1;
	ret = rtos_create_thread(&bkwl_main->task.msg,
							 BEKEN_DEFAULT_WORKER_PRIORITY,
							 "task.msg",
							 (beken_thread_function_t)bkwl_event_message_handle,
							 1024 * 100,
							 NULL);

	while (1)
	{

		if (num == 1)
		{
			bkwl_event_send_cmd(BKWL_CMD_BOARD_INIT);
		}

#if 1
		if (num == 2)
		{
			bkwl_event_send_cmd(BKWL_WIFI_DATA_CONNECT);
		}
#endif

#if 1
		if (num == 2)
		{
			bkwl_event_send_cmd(BKWL_CMD_CAMERA_INIT);
		}
#endif
#if 0
		if (num % 15 == 0)
		{
			bkwl_event_send_cmd(BKWL_CMD_HTTP_POST_IMAGE);
		}
#endif

		if (num % 5 == 0)
		{
			if (bkwl_main->status.mqtt_connect == 1)
			{
				if (strlen(bkwl_main->bt.token) > 2)
				{
					tprintf("send pair topic token: %s\n", bkwl_main->bt.token);
					bkwl_mqtt_sibo_pair_net(bkwl_main->bt.token);
					memset(bkwl_main->bt.token, 0, 64);
				}
			}
		}

#if 0
		if (num % 5 == 0)
		{
		
			bkwl_event_send_cmd(BKWL_CMD_LCD_DISPLAY);
		}
#endif

#if 1
		if (num % 35 == 0)
		{
			if (bkwl_main->status.mqtt_connect == 1)
				bkwl_event_send_cmd(BKWL_CMD_MQTT_SEND_DATA);
		}
#endif

		if (num % 65 == 0)
		{
			tprintf("twh bkwl_event_main_handle num=%d\n", num);
		}

		if (num > 0xffff)
			num = 0;
		num++;
		rtos_delay_milliseconds(1000);
	}

	return ret;
}
